package com.chinasoft.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class JedisPoolUtil {
    private static JedisPool jedisPool;
    static {
        InputStream resourceAsStream = JedisPoolUtil.class.getClassLoader().getResourceAsStream("redis.properties");
        Properties pro = new Properties();
        try {
            pro.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(Integer.parseInt(pro.getProperty("redis.maxIdle")));
        jedisPoolConfig.setMinIdle(Integer.parseInt(pro.getProperty("redis.minIdle")));
        //初始化连接对象
        jedisPool = new JedisPool(jedisPoolConfig,pro.getProperty("redis.host"),Integer.parseInt(pro.getProperty("redis.port")));
        System.out.println("redis.host"+pro.getProperty("redis.host"));
        System.out.println("redis.port"+pro.getProperty("redis.port"));
    }

    /**
     * 获取连接对象
     * @return
     */
    public static Jedis getJedis(){

        return jedisPool.getResource();

    }

}
